<?php
    $ra_id = $_POST['ra_id'];
    $news_title = addslashes($_POST['news_title']);
    $news_msg = addslashes($_POST['news_msg']);
    $news_date = strtotime($_POST['news_date']);
    
    $sql = "UPDATE relate_article SET
    ra_date='$news_date',
    ra_title ='$news_title',
    ra_msg ='$news_msg' WHERE ra_id=$ra_id";
    $result = $db->Execute($sql);
    
    if($result){
        $msg = 'บันทึกข้อมูลเรียบร้อยแล้ว';
        if($_FILES['news_img']['name']!='')
        {
            $handle = new Upload($_FILES['news_img']);
            if ($handle->uploaded)
            {
                $handle->image_resize = true;
                $handle->image_ratio_fill = true;
                $handle->image_background_color = '#ffffff';
                $handle->image_x = 254;
                $handle->image_y = 134;
                $handle->Process(APPPATH.'/images/relate-article/'.$ra_id);
                
                if ($handle->processed)
                {
                    $sql = 'UPDATE relate_article SET ra_img="'.$handle->file_dst_name.'" WHERE ra_id='.$ra_id;
                    $result = $db->Execute($sql);
                    if($result)
                    {
                        $msg = 'บันทึกข้อมูลเรียบร้อยแล้ว';
                        if(img_exists(APPPATH.'/images/relate-article/'.$ra_id,$_POST['img_old'])){
                            @unlink(APPPATH.'/images/relate-article/'.$ra_id.'/'.$_POST['img_old']);
                        }
                    }
                    else
                    {
                        $msg = 'ไม่สามารถอัพโหลดรูปได้';
                        if(img_exists(APPPATH.'/images/relate-article/'.$ra_id,$handle->file_dst_name)){
                            @unlink(APPPATH.'/images/relate-article/'.$ra_id.'/'.$handle->file_dst_name);
                        }
                    }
                }
                else
                {
                    $msg = 'ไม่สามารถอัพโหลดรูปได้';
                }
    
            }
            else
            {
                $msg = 'ไม่สามารถอัพโหลดรูปได้';
            }
        }
    }else{
        $msg = 'ไม่สามารถบันทึกข้อมูลได้';
    }
?>

<?php
    include('header.php');
?>
<div class="grid_24" id="contents">
    <div id="query_result">
    <?php
        echo $msg , ' <img src="images/ajax-loader.gif" />';
        redirect('?m=relate-article&s=news-edit&ra_id='.$ra_id,3);
    ?>
    </div>
</div><!-- end .grid_18 -->
<div class="clear">&nbsp;</div>
<?php
    include('footer.php');
?>